//
//  OosMe.m
//  oos4ios
//
//  Created by Pablo Gonzalez on 4/12/11.
//  Copyright 2011 11870.com. All rights reserved.
//

#import "OosMe.h"
#import <oos4ios/Model/OosMe.h>
#import <oos4ios/Model/OosService.h>
#import <oos4ios/Oos4ios.h>
#import "../../Persistence/OosManagedContext.h"
#import "../../Persistence/OosManagedObjectMover.h"

@implementation OosMe

static NSString *fetchPredicate = @"slug = %@";

+ (OosMe *) meFromSlug: (NSString *) userSlug
{
    OosMe *me = [super objectWithPredicate: fetchPredicate, userSlug];
    
    if (!me) {
        me = [super create];
        me.slug = userSlug;
    }
    
    return me;
}

static OosMe *me = nil;

+ (OosMe *) me
{
    OosMe *newMe =  nil;
    if ([[Oos4ios instance] authenticated]) {
        if (!me || [me managedObjectContext] == nil) {
            newMe = [[super objectWithPredicate:nil] retain];
            [me release];
            me = newMe;
        }
        if (me) {
            newMe = [[OosManagedObjectMover moveObjectToCurrentContext:me] retain];
            [me release];
            me = newMe;
        }
    } else {
        me = nil;
    }
    return me;
}

+ (void)clearMe
{
    me = nil;
}

@dynamic editAvatarUrlString;

- (NSURL *) editAvatarUrl
{
    NSString* urlString = [self editAvatarUrlString];
    if (urlString) {
        return [NSURL URLWithString: urlString];
    }
    return nil;
}

- (void) setEditAvatarUrl: (NSURL *) url
{
    if (url) {
        self.editAvatarUrlString = [url absoluteString];
    }
}

@dynamic name;
@dynamic surname;
@dynamic telephone;
@dynamic mail;

@dynamic facebook;
@dynamic twitter;
@dynamic fbCheckin;

- (BOOL) hasFacebook 
{
    return self.facebook.boolValue;
}
- (BOOL) hasTwitter
{
    return self.twitter.boolValue;
}
- (BOOL) hasFbCheckin
{
    return self.fbCheckin.boolValue;
}

@end
